package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.MenuMeal;
import com.mdd.common.entity.order.OrderMeal;
import com.mdd.common.entity.order.OrderMenu;
import com.mdd.common.mapper.order.MenuMealMapper;
import com.mdd.common.mapper.order.OrderMealMapper;
import com.mdd.common.mapper.order.OrderMenuMapper;
import com.mdd.common.util.AmountUtils;
import com.mdd.front.service.IMenuService;
import com.mdd.front.validate.commons.PageValidate;
import com.mdd.front.validate.order.MenuSearchValidate;
import com.mdd.front.vo.order.MenuMealVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shenjian
 * @createTime 2023年02月21日 23:21:00
 */
@Service
public class MenuServiceImpl implements IMenuService {

    @Resource
    OrderMenuMapper orderMenuMapper;

    @Resource
    OrderMealMapper orderMealMapper;

    @Resource
    MenuMealMapper menuMealMapper;

    @Override
    public PageResult<MenuMealVo> list(PageValidate pageValidate) {
        Integer pageNo = pageValidate.getPageNo();
        Integer pageSize = pageValidate.getPageSize();
        Page<MenuMeal> iPage = new Page<>();
        List<MenuMealVo> list = new ArrayList<>();
        OrderMenu orderMenu = this.orderMenuMapper.selectOne(new QueryWrapper<OrderMenu>()
                .eq("status", 1)
                .last("limit 1"));
        if (orderMenu != null && orderMenu.getId() != null) {
            List<String> orderList = new ArrayList<>();
            orderList.add("seq");
            orderList.add("id");
            // 查询菜单关联的套餐
            iPage = this.menuMealMapper.selectPage(
                    new Page<>(pageNo, pageSize),
                    new QueryWrapper<MenuMeal>()
                            .orderByAsc(orderList)
                            .eq("is_delete", 0)
                            .eq("menu_id", orderMenu.getId())
            );
            List<Integer> mealIdList = iPage.getRecords().stream()
                    .map(MenuMeal::getMealId)
                    .collect(Collectors.toList());
            if (mealIdList.isEmpty()) {
                return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
            }
            List<OrderMeal> meals = this.orderMealMapper.selectList(new QueryWrapper<OrderMeal>()
                    .in("id", mealIdList));
            Map<Integer, List<OrderMeal>> mealMap = meals.stream().collect(Collectors.groupingBy(OrderMeal::getId));

            for (MenuMeal menuMeal : iPage.getRecords()) {
                MenuMealVo vo = MenuMealVo.builder().build();
                final OrderMeal meal = mealMap.get(menuMeal.getMealId()).get(0);
                BeanUtils.copyProperties(menuMeal, vo);
                vo.setMenuMealId(menuMeal.getId());
                vo.setMealName(meal.getMealName());
                vo.setMealPic(meal.getMealPic());
                vo.setMealDescribe(meal.getMealDescribe());
                vo.setPrice(AmountUtils.changeF2Y(menuMeal.getPrice()));
                vo.setPreferentialAmount(AmountUtils.changeF2Y(menuMeal.getPreferentialAmount()));
                vo.setActualAmount(AmountUtils.changeF2Y(menuMeal.getPrice() - menuMeal.getPreferentialAmount()));
                vo.setSalesNum(menuMeal.getSalesNum());
                // 如果套餐是上架状态，判断库存是否为0
                if (vo.getStatus() == 1) {
                    // 如果库存为0，套餐状态为下架状态
                    // 如果库存不为0，套餐状态为上架状态
                    vo.setStatus(vo.getStock() == 0 ? 0 : 1);
                }
                list.add(vo);
            }
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }
}
